package com.zkyc.framework.website.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkyc.framework.common.pojo.FileVerification;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.vo.website.DeleteVo;
import com.zkyc.framework.website.service.FileVerificationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 防篡改-文件校验信息
 */
@RequestMapping(("fileVerification"))
@RestController
public class FileVerificationController {


    @Autowired
    private FileVerificationService fileVerificationService;

    /**
     * 保存
     *
     */
    @PostMapping("save")
    public ResultVO save(@RequestBody FileVerification fileVerification) {
        return fileVerificationService.save(fileVerification) ? ResultVO.success("保存成功！") : ResultVO.fail("保存失败！");
    }

    /**
     * 批量保存
     *
     */
    @PostMapping("saveBatch")
    public ResultVO saveBatch(@RequestBody List<FileVerification> fileVerifications) {
        return fileVerificationService.saveBatch(fileVerifications) ? ResultVO.success("保存成功！") : ResultVO.fail("保存失败！");
    }


    /**
     * 根据id删除
     * @return 查询结果
     */
    @PostMapping("delete")
    public ResultVO delete(@RequestBody DeleteVo deleteVo) {
        LambdaUpdateWrapper<FileVerification> luw = new LambdaUpdateWrapper<>();
        luw.in(!CollectionUtils.isEmpty(deleteVo.getIds()), FileVerification::getId, deleteVo.getIds());
        luw.in(!CollectionUtils.isEmpty(deleteVo.getCids()), FileVerification::getCid, deleteVo.getCids());
        return fileVerificationService.remove(luw) ? ResultVO.success("删除成功！") : ResultVO.fail("删除失败！");
    }

    /**
     * 查询
     *
     * @return 查询结果
     */
    @GetMapping("list")
    public ResultVO list(
            @RequestParam(value = "cid", required = false, defaultValue = "-1")int cid,
            @RequestParam(value = "search", required = false, defaultValue = "")String search,
            @RequestParam(value = "current", required = false, defaultValue = "-1") int current,
            @RequestParam(value = "size", required = false, defaultValue = "-1") int size
    ) {
        LambdaQueryWrapper<FileVerification> lqw = new LambdaQueryWrapper<>();

        lqw.eq(cid > 0, FileVerification::getCid, cid);

        if (StringUtils.isNotBlank(search)) {
            lqw.like(FileVerification::getCid, search)
                    .or().like(FileVerification::getFileName, search)
                    .or().like(FileVerification::getPath, search);
        }

        IPage<FileVerification> page = new Page<>(current, size);;

        if (current > 0 && size > 0) fileVerificationService.page(page, lqw);
        else {
            List<FileVerification> list = fileVerificationService.list(lqw);
            page.setRecords(list);
            page.setTotal(list.size());
            page.setCurrent(1);
            page.setSize(list.size());
        }
        return ResultVO.data(page);
    }

    /**
     * 根据id查询
     *
     * @return 查询结果
     */
    @GetMapping("getById")
    public ResultVO getById(Integer id) {
        return ResultVO.data(fileVerificationService.getById(id));
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public ResultVO update(@RequestBody FileVerification fileVerification) {
        return fileVerificationService.updateById(fileVerification) ? ResultVO.success("修改成功！") : ResultVO.fail("修改失败！");
    }


}

